package com.qianfeng.lingshijia;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import adapter.ImportListAdapter;
import adapter.TotalListReportAdapter;
import entity.TotalList_Import_Entity;
import entity.TotalList_Report_Entity;
import utils.DownloadUtils;

public class TotalListActivity_Report extends AppCompatActivity
{

    String URL = "http://cmsproxy.haolingzui.com/reportList.do";
    int pageNumber = -1;
    PullToRefreshListView totallist_reportList;
    TotalListReportAdapter totalListReportAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_total_list_activity__report);

        Intent intent = getIntent();
        if (intent!=null)
        {
            URL = intent.getStringExtra("url");
        }
        initView();
        loadData();
    }

    /**
     * 初始化控件
     */
    private void initView()
    {
        totallist_reportList = (PullToRefreshListView) findViewById(R.id.totallist_report_List);
        totalListReportAdapter = new TotalListReportAdapter(getApplicationContext());
        totallist_reportList.setAdapter(totalListReportAdapter);
        refresh(totallist_reportList);
    }

    private void refresh(PullToRefreshListView totallist_reportList)
    {
        totallist_reportList.setMode(PullToRefreshBase.Mode.BOTH);
        ILoadingLayout startLabels = totallist_reportList.getLoadingLayoutProxy(true,false);
        startLabels.setPullLabel("下拉刷新...");// 刚下拉时，显示的提示
        startLabels.setRefreshingLabel("正在载入...");// 刷新时
        startLabels.setReleaseLabel("放开刷新...");// 下来达到一定距离时，显示的提示

        // 设置上拉刷新文本
        ILoadingLayout endLabels = totallist_reportList.getLoadingLayoutProxy(false, true);
        endLabels.setPullLabel("上拉刷新...");// 刚下拉时，显示的提示
        endLabels.setRefreshingLabel("正在载入...");// 刷新时
        endLabels.setReleaseLabel("放开刷新...");// 下来达到一定距离时，显示的提示

        totallist_reportList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>()
        {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView)
            {
                //下拉
                pageNumber = -1;
                totalListReportAdapter.clearData();
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView)
            {
                //上拉
                loadData();
            }
        });
    }


    /**
     * 数据加载
     */
    private void loadData()
    {
        pageNumber ++;
        //热销单品数据加载
        RequestParams params = new RequestParams();
        //page=0&pageSize=9&sortOrder=0&sortType=1
        params.addBodyParameter("page",pageNumber+"");
        params.addBodyParameter("pageSize", "20");

        DownloadUtils.postJson(URL, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                if (responseInfo != null) {
                    String response = responseInfo.result;
                    Gson gson = new Gson();
                    TotalList_Report_Entity entity = gson.fromJson(response, TotalList_Report_Entity.class);
                    //totalListReportAdapter.setData(entity.getData().getList());
                    totalListReportAdapter.addData(entity.getData().getList());
                }
                totallist_reportList.onRefreshComplete();
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                error.printStackTrace();
                totallist_reportList.onRefreshComplete();
            }
        }, params);
    }

    /**
     * 点击回退返回
     * @param view
     */
    public void back(View view)
    {
        this.finish();
    }

}